#include "src/ltlog.h"

#include <stdarg.h>
#include "src/ltbase.h"
#include "src/ltgen.h"

lt_UsingType(lt, U32);

static ltlog_Level g_Level = ltlog_Info;

ltlog_Level
ltlog_GetLevel(void) {
    return g_Level;
}

ltlog_Level
ltlog_SetLevel(ltlog_Level level) {
    ltlog_Level old = g_Level;
    g_Level = level;
    return old;
}

static int
ltgen_gen_ltlog_Logs_Each(lt_U32 n) {
    printf("#define ltlog_Log%" lt_FmtU32 "(M_level, M_text", n);

    for (lt_U32 i = 0; i < n; ++i) {
        printf(", M_l%" lt_FmtU32 ", M_x%" lt_FmtU32, i + 1, i + 1);
    }

    printf(") \\\n"
           "    do { \\\n"
           "        ltlog__Start(M_level, M_text); \\\n"
           "        \\\n");

    for (lt_U32 i = 0; i < n; ++i) {
        printf("        ltlog__ShowExpr(M_l%" lt_FmtU32 ", M_x%" lt_FmtU32
               "); \\\n",
               i + 1, i + 1);
    }

    if (n != 0) {
        printf("        \\\n");
    }

    printf("        ltlog__End_##M_level(); \\\n"
           "    } while (0)\n\n");

    return 0;
}

ltgen_Generator(ltlog_Logs) {
    int r;

    printf("/* clang-format off */\n");
    for (U32 i = 0; i < 8; ++i) {
        r = ltgen_gen_ltlog_Logs_Each(i);
        if (r != 0)
            return r;
    }
    printf("/* clang-format on */\n");

    return 0;
}
